﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using RRRSRoguelike.Managers;
using RRRSRoguelike.Screens;

namespace RRRSRoguelike.Helpers.Visualizer
{
    /// <summary>
    /// Contains the code for displaying text within the game panels.
    /// </summary>
    public class Panel
    {

        #region variables
        private Point firstCharacter;
        private int lengthInCharacters;

        private string currentString;
        private ConsoleColor currentColor;
        private TextAlignment currentTextAlignment;

        string borderCharacter;
        ConsoleColor borderColor;

        int height;
        int width;
        
        Visualizer dungeonVisualizer = DungeonManager.Instance.DungeonVisualizer;
        
        /// <summary>
        /// Top left corner.
        /// </summary>
        Point position;
        #endregion

        #region constructor
        /// <summary>
        /// Creates an instance of panel
        /// </summary>
        /// <param name="point"></param>
        /// <param name="width"></param>
        public Panel(Point position, int width,int height, 
            string borderCharacter = "+", ConsoleColor borderColor = ConsoleColor.White)
        {
            currentString = "";
            currentColor = ConsoleColor.White;
            this.position = position;
            firstCharacter = new Point(position.X+2,position.Y+2);
            lengthInCharacters = width-4;
            this.height = height;
            this.width = width;
            this.borderCharacter = borderCharacter;
            this.borderColor = borderColor;
        }
        #endregion

        #region methods
        public void Clear() 
        { 
            //doesn't work
            StringBuilder horizontaLine = new StringBuilder();

            //prepareline
            for (int i = 3; i < width-3; i++)
                horizontaLine.Append(" ");

            string line = horizontaLine.ToString();

            for (int j = 2; j < height-2; j++)
                horizontaLine.AppendLine(line);

               Write (horizontaLine.ToString(),ConsoleColor.White,TextAlignment.Multi);
        }

        //maybe create an enum for singleline, multi, centred.
        public void Write(string text, ConsoleColor color = ConsoleColor.White, TextAlignment textAlignment = TextAlignment.Single,                     StringSplitOptions options = StringSplitOptions.None)
        {
            //only draw if changed
            if (currentColor != color || currentString != text|| currentTextAlignment != textAlignment)
            {
                currentColor = color; currentString = text; currentTextAlignment = textAlignment;

                switch(textAlignment)
                {
                    case TextAlignment.Single:
                        if (text.Length > width)
                            text = text.Remove(width);
                        Visualizer.WriteLine(text,firstCharacter,color);
                        break;
                    case TextAlignment.Center:
                        text = Visualizer.CentralizeText(text,lengthInCharacters);
                        Visualizer.WriteLine(text, firstCharacter, color);
                        break;
                    case TextAlignment.Multi:
                        Visualizer.WriteMultiLine(
                    Visualizer.BuildString(text, lengthInCharacters).ToString(), firstCharacter,color,options);
                        
                    break;
                }

                
            }
        }

        public void Draw()
        {
            StringBuilder horizontaLine = new StringBuilder();

            //prepareline
            for (int i = 0; i < width; i++)
                horizontaLine.Append(borderCharacter);
            //Top
            Visualizer.WriteLine(horizontaLine.ToString(), position, borderColor);

            //Bottom
            Visualizer.WriteLine(horizontaLine.ToString(),
                new Point(position.X, position.Y + height - 1), borderColor);


            for (int i = 0; i < height; i++)
            {
                //Left
                Visualizer.WriteLine(borderCharacter,
                        new Point(position.X, position.Y + i), borderColor);
                //Right
                Visualizer.WriteLine(borderCharacter,
                new Point(position.X + width, position.Y + i), borderColor);
            }

        }
        #endregion

    }
}
